package com.lzx.shop.service.impl;

import com.lzx.shop.dao.CategoryDao;
import com.lzx.shop.dataobject.CategoryDO;
import com.lzx.shop.service.CategoryService;
import com.lzx.shop.utils.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName CategoryServiceImpl.java
 * @Description TODO
 * @createTime 2021年03月17日 23:38:00
 */
@Service
public class CategoryServiceImpl  implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);

    /**
     * 报错或者更新
     * @param categoryDO
     * @return
     */
    @Override
    public boolean save(CategoryDO categoryDO) {
        if (categoryDO==null){
            return false;
        }
        if (!StringUtils.hasText(categoryDO.getId())){
            categoryDO.setId(UUIDUtil.getUUID());
            return categoryDao.insert(categoryDO)>0;
        }
        return categoryDao.updateById(categoryDO)>0;

    }

    @Override
    public List<CategoryDO> findAll() {
        return categoryDao.selectList(null);
    }

    @Override
    public CategoryDO findById(String id) {
        return categoryDao.selectById(id);
    }

    @Override
    public List<CategoryDO> findByIds(List<String> ids) {
        return categoryDao.selectBatchIds(ids);
    }

    @Override
    public boolean update(CategoryDO categoryDO) {
        if (categoryDO==null){
            LOG.error("input categoryDO is blank");
            return false;
        }
        int result =categoryDao.updateById(categoryDO);
        return result>0;
    }

    @Override
    public boolean DeleteById(String id) {
        if (!StringUtils.hasText(id)){
            LOG.error("input id is blank");
            return false;
        }
        return categoryDao.deleteById(id)>0;
    }

    @Override
    public Integer count() {
        return categoryDao.selectCount(null);
    }
}
